package com.ruoyi.combat.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.json.JSONUtil;
import com.ruoyi.combat.domain.neo4j.CombatAtlasTask;
import com.ruoyi.combat.domain.useralignment.CombatAlignmentWarn;
import com.ruoyi.combat.domain.useralignment.CombatHeavyUser;
import com.ruoyi.combat.domain.userlocation.CombatUser;
import com.ruoyi.combat.domain.userlocation.CombatWarn;
import com.ruoyi.combat.domain.vo.RiskDataStatisticsVo;
import com.ruoyi.combat.mapper.equiplocation.CombatEquipInfoMapper;
import com.ruoyi.combat.mapper.useralignment.CombatAlignmentWarnMapper;
import com.ruoyi.combat.mapper.useralignment.CombatHeavyUserMapper;
import com.ruoyi.combat.mapper.userlocation.CombatUserMapper;
import com.ruoyi.combat.mapper.userlocation.CombatWarnMapper;
import com.ruoyi.combat.repository.CombatAtlasTaskRepository;
import com.ruoyi.combat.repository.ForecastOrgNodeRepository;
import com.ruoyi.combat.repository.IpNodeRepository;
import com.ruoyi.combat.service.antihoming.AntiHomingInfoService;
import com.ruoyi.combat.service.apprisk.CombatApplicationDataService;
import com.ruoyi.combat.service.equiplocation.ICombatEquipInfoService;
import com.ruoyi.common.utils.DateUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.mapper.CombatCountMapper;
import com.ruoyi.combat.domain.CombatCount;
import com.ruoyi.combat.service.ICombatCountService;

/**
 * 统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class CombatCountServiceImpl implements ICombatCountService 
{
    @Autowired
    private CombatCountMapper combatCountMapper;

    @Autowired
    private CombatApplicationDataService combatApplicationDataService;

    @Autowired
    private CombatUserMapper combatUserMapper;

    @Autowired
    private CombatWarnMapper combatWarnMapper;
    @Autowired
    private CombatHeavyUserMapper combatHeavyUserMapper;
    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private IpNodeRepository ipNodeRepository;
    @Autowired
    private ForecastOrgNodeRepository forecastOrgNodeRepository;
    @Autowired
    private CombatAtlasTaskRepository combatAtlasTaskRepository;
    @Autowired
    private ICombatEquipInfoService combatEquipInfoService;

    /**
     * 网关会回显数据统计
     *
     * @return 统计
     */
    @Override
    public void CountDataByDay() {
        CombatCount combatCount = new CombatCount();
        //课题一统计数据
        Map<String, Long> num = combatApplicationDataService.getNum();
        combatCount.setTopic1(JSONUtil.toJsonStr(num));

        //课题二统计数据
        RiskDataStatisticsVo riskDataStatisticsVo = combatEquipInfoService.riskDataStatistics();
        int highRiskCount = riskDataStatisticsVo.getHighRiskCount();
        int mediumRiskCount = riskDataStatisticsVo.getMediumRiskCount();
        int lowRiskCount = riskDataStatisticsVo.getLowRiskCount();
        Map<String,Object> data2 = new HashMap<>();
        data2.put("totalCount", highRiskCount + mediumRiskCount + lowRiskCount);// 总数
        data2.put("warn", highRiskCount + mediumRiskCount);// 中高风险数
        data2.put("success", lowRiskCount);// 低风险数
        combatCount.setTopic2(JSONUtil.toJsonStr(data2));

        //课题三统计数据
        List<CombatUser> combatUsers = combatUserMapper.selectCombatUserList(new CombatUser());
        int size = combatUsers.size();
        CombatWarn combatWarn = new CombatWarn();
        List<CombatWarn> combatWarns = combatWarnMapper.selectCombatWarnList(combatWarn);
        combatWarn.setWarnState("1");
        List<CombatWarn> combatWarns1 = combatWarnMapper.selectCombatWarnList(combatWarn);
        Map<String,Object> data3 = new HashMap<>();
        data3.put("userNumber", size); //重保用户总数
        data3.put("warn", combatWarns.size());//重保用户位置泄露告警总数
        data3.put("success", combatWarns1.size());//保护重保用户位置泄露告警保护成功总数
        combatCount.setTopic3(JSONUtil.toJsonStr(data3));

        //课题四统计数据
        Map<String,Object> data4 = new HashMap<>();
        long totalCount = ipNodeRepository.count();
        long belongsCount = ipNodeRepository.countByBelongsTo();
        long preCount = forecastOrgNodeRepository.count();
        long finishedCount = combatAtlasTaskRepository.findFinishedByAtlasTask();
        data4.put("totalCount", totalCount);// 总数
        // warn 为 belongsCount + preCount
        data4.put("warn", belongsCount + preCount);// 确定风险 + 预测风险数
        data4.put("success", finishedCount);// 已完成抗归属数
        combatCount.setTopic4(JSONUtil.toJsonStr(data4));

        //课题五统计数据
        Map<String,Object> data5 = new HashMap<>();
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(new CombatHeavyUser());
        data5.put("userNumber", combatHeavyUsers.size());//重保用户总数
        CombatAlignmentWarn combatAlignmentWarn = new CombatAlignmentWarn();
        List<CombatAlignmentWarn> warnList = combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn);
        data5.put("warn", warnList.size());//用户信息泄露告警总数
        combatAlignmentWarn.setStatus("1");
        List<CombatAlignmentWarn> combatAlignmentWarns = combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn);
        data5.put("success", combatAlignmentWarns.size());//用户信息泄露告警保护成功总数
        combatCount.setTopic5(JSONUtil.toJsonStr(data5));

        combatCount.setCreateTime(new Date());
        combatCountMapper.insertCombatCount(combatCount);

    }

    /**
     * 查询统计
     * 
     * @param id 统计主键
     * @return 统计
     */
    @Override
    public CombatCount selectCombatCountById(Long id)
    {
        return combatCountMapper.selectCombatCountById(id);
    }

    /**
     * 查询统计列表
     * 
     * @param combatCount 统计
     * @return 统计
     */
    @Override
    public List<CombatCount> selectCombatCountList(CombatCount combatCount)
    {
        return combatCountMapper.selectCombatCountList(combatCount);
    }

    /**
     * 新增统计
     * 
     * @param combatCount 统计
     * @return 结果
     */
    @Override
    public int insertCombatCount(CombatCount combatCount)
    {
        combatCount.setCreateTime(DateUtils.getNowDate());
        return combatCountMapper.insertCombatCount(combatCount);
    }

    /**
     * 修改统计
     * 
     * @param combatCount 统计
     * @return 结果
     */
    @Override
    public int updateCombatCount(CombatCount combatCount)
    {
        combatCount.setUpdateTime(DateUtils.getNowDate());
        return combatCountMapper.updateCombatCount(combatCount);
    }

    /**
     * 批量删除统计
     * 
     * @param ids 需要删除的统计主键
     * @return 结果
     */
    @Override
    public int deleteCombatCountByIds(Long[] ids)
    {
        return combatCountMapper.deleteCombatCountByIds(ids);
    }

    /**
     * 删除统计信息
     * 
     * @param id 统计主键
     * @return 结果
     */
    @Override
    public int deleteCombatCountById(Long id)
    {
        return combatCountMapper.deleteCombatCountById(id);
    }
}
